package com.zygh.policeasset.component;

import cn.hutool.core.date.DateTime;
import com.zygh.policeasset.mapper.AssetsNoMapper;
import com.zygh.policeasset.mapper.SealOrderNoMapper;
import com.zygh.policeasset.pojo.AssetsNo;
import com.zygh.policeasset.pojo.SealOrderNo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

@Component
public class SealOnOrderCode {

    @Autowired
    SealOrderNoMapper sealOrderNoMapper;

    public String getCode(Integer type) {
        String typeCode = null;
        if (type == 0) {
            typeCode = "ZN";
        } else {
            typeCode = "JC";
        }
        DateTime dateTime = DateTime.now();
        Integer count = sealOrderNoMapper.findByType(typeCode, dateTime.year());

        if(count == 0){
            sealOrderNoMapper.add(typeCode,0,dateTime.year());
        }
        SealOrderNo sealOrderNo = sealOrderNoMapper.findByTypeAndYear(typeCode, dateTime.year());

        StringBuffer code = new StringBuffer();
        code.append(typeCode);
        code.append(sealOrderNo.getYear());
        for (int i = 1; i < 5 - String.valueOf(sealOrderNo.getCounter()).length(); i++) {
            code.append("0");
        }
        code.append(sealOrderNo.getCounter()+1);

        sealOrderNoMapper.update(typeCode, dateTime.year());
        return code.toString();
    }
}
